/*ParallelPlatform Project - A web platform to distribute and share knowledge
Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/
GENTICS.Aloha.Sections = new GENTICS.Aloha.Plugin("com.parallelplatform.aloha.plugins.Sections");
GENTICS.Aloha.Sections.languages = ["en", "es"];
GENTICS.Aloha.Sections.sectionCode = [];
GENTICS.Aloha.Sections.buttonCode = null;


GENTICS.Aloha.Sections.init = function () {
	this.initButtons();
	var that = this;
	GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, "selectionChanged", function (e, params) {
		var range = GENTICS.Aloha.Selection.getRangeObject();
		if( range.isCollapsed() && !GENTICS.Aloha.Sections.findTitleMarkup(range) ){
			if( GENTICS.Aloha.Sections.findSectionMarkup(range) ){
				for (var button in that.buttonsSection)
					that.buttonsSection[button].show();
			}else{
				for (var button in that.buttonsSection)
					that.buttonsSection[button].hide();
				that.buttonsSection["addsection"].show();
			}
			that.buttonCode.show();
		}else{
			for (var button in that.buttonsSection)
				that.buttonsSection[button].hide()
			that.buttonCode.hide();
		}
		GENTICS.Aloha.FloatingMenu.doLayout();
	});

	//Section Codes
	
	$.each( $("code"), function(){
		GENTICS.Aloha.Sections.InitSecionCode(this);
	});
	
	prettyPrint();
};

GENTICS.Aloha.Sections.NewCodeDialog = function( idCode ){
	var dialog = jQuery('<div class="totalPanel"><div class="dialogPanel dialogCode">'+
			   '<div id="dialogcode"><textarea>'+ this.i18n("message.newCode") +'</textarea></div>'+
               '<div class="subMenu"><input type="button" name="ok" value="'+ this.i18n("input.ok") +'">'+
               '</div>'+
               '</div></div>');
               
	dialog.find("textarea").val(this.addTags(this.sectionCode[idCode]));
	
	dialog.find('input[name=ok]').click( function(){
		dialog.find("textarea").val(GENTICS.Aloha.Sections.removeTags(dialog.find("textarea").val()));
		$('code[idsectioncode='+idCode+']').find("pre").html(dialog.find("textarea").val());
		GENTICS.Aloha.Sections.sectionCode[idCode] = dialog.find("textarea").val();
		prettyPrint();
	 	$(".totalPanel").remove();
	});
	
	$("body").append(dialog);
	
	dialog.find("textarea").focus();
	
}

GENTICS.Aloha.Sections.addTags = function(cadena){
	return cadena.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}

GENTICS.Aloha.Sections.removeTags = function(cadena){
	return cadena.replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

GENTICS.Aloha.Sections.stripHTML = function (cadena)
{
    return cadena.replace(/<[^>]+>/g,'');
}

GENTICS.Aloha.Sections.InitSecionCode = function(code){
	code = $(code);
	code.attr("contenteditable","false");
	code.attr("idsectioncode",this.sectionCode.length);
	var idCode = this.sectionCode.length;
	this.sectionCode.push(this.stripHTML(code.find("pre").html()));
	code.find("pre").addClass("prettyprint");
	code.click(function(event){
		GENTICS.Aloha.Sections.NewCodeDialog(idCode);
		GENTICS.Aloha.FloatingMenu.setScope("GENTICS.Aloha.empty");
	});
}

GENTICS.Aloha.Sections.CodeOriginal = function(){
	var that = GENTICS.Aloha.Sections; 
	$.each( $("code"), function(){
		$(this).find("pre").html( that.sectionCode[$(this).attr("idsectioncode")] )
	});	
}

GENTICS.Aloha.Sections.findCodeMarkup = function( range ) {
	if (typeof range == "undefined") {
		var range = GENTICS.Aloha.Selection.getRangeObject()
	}
	return range.findMarkup( function() { //This in section, delete section
								return this.nodeName.toLowerCase() == 'pre';
							} 
				, jQuery(GENTICS.Aloha.activeEditable.obj))
};

GENTICS.Aloha.Sections.findTitleMarkup = function() {
	if (GENTICS.Aloha.activeEditable)
		return GENTICS.Aloha.activeEditable.getId() == "title"
	else
		return false;
};

GENTICS.Aloha.Sections.findSectionMarkup = function( range ) {
	if (typeof range == "undefined") {
		var range = GENTICS.Aloha.Selection.getRangeObject()
	}
	return range.findMarkup( function() { //This in section, delete section
								return this.nodeName.toLowerCase() == 'section';
							}
				, jQuery(GENTICS.Aloha.activeEditable.obj))
};


GENTICS.Aloha.Sections.initButtons = function () {
	var scope = "GENTICS.Aloha.continuoustext";
	
	this.buttonsSection = {};
	var that = this;
	var button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button sectionBtn",
							size : "small",
							//label: that.i18n("button.addSection"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								//var section = GENTICS.Aloha.Sections.findSectionMarkup(range);
								if( range.isCollapsed() ){
									/*if( section ){
										GENTICS.Utils.Dom.removeFromDOM(section, range, true);									
									}else{ // out section, add new section*/
										section = jQuery("<section><h1>" + that.i18n("message.newSection") + "</h1><p> " + that.i18n("message.newContent") + "</p></section>");
										GENTICS.Utils.Dom.insertIntoDOM(section, range, jQuery(GENTICS.Aloha.activeEditable.obj) ,true);
									//}
								}else
									GENTICS.Aloha.Log.warn(GENTICS.Aloha.Sections, "this feature is not available with multiple choice");
								
							},
							tooltip : that.i18n("button.addSection"),
							toggle : false
						})
	
	GENTICS.Aloha.FloatingMenu.addButton(scope, button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 2);
	this.buttonsSection["addsection"] = button;
	
	button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button sectionRemoveBtn",
							size : "small",
							//label: that.i18n("button.removeSection"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								var section = GENTICS.Aloha.Sections.findSectionMarkup(range);
								if( range.isCollapsed() ){
									if( section )
										GENTICS.Utils.Dom.removeFromDOM(section, range, true);												
								}else
									GENTICS.Aloha.Log.warn(GENTICS.Aloha.Sections, "this feature is not available with multiple choice");
							},
							tooltip : that.i18n("button.removeSection"),
							toggle : false
						});
	
	GENTICS.Aloha.FloatingMenu.addButton(scope, button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 2);
	this.buttonsSection["removesection"] = button;
	
	button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button sectionBeforeBtn",
							size : "small",
							//label: that.i18n("button.addBeforeSection"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								var section = GENTICS.Aloha.Sections.findSectionMarkup(range);
								if( range.isCollapsed() && section ){
									var newSection = jQuery("<section><h1>New Section</h1><p>Content in the new section</p></section>").get(0);
									section.parentNode.insertBefore(newSection, section);
								}else
									GENTICS.Aloha.Log.warn(GENTICS.Aloha.Sections, "this feature is not available with multiple choice or out of section");
							},
							tooltip : that.i18n("button.addBeforeSection"),
							toggle : false
						})
	
	GENTICS.Aloha.FloatingMenu.addButton(scope, button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 2);
	this.buttonsSection["addbeforesection"] = button;
	
	button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button sectionAfterBtn",
							size : "small",
							//label : that.i18n("button.addNextSection"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								var section = GENTICS.Aloha.Sections.findSectionMarkup(range);
								if( range.isCollapsed() && section ){
									var newSection = jQuery("<section><h1>New Section</h1><p>Content in the new section</p></section>").get(0);
									section.parentNode.insertBefore(newSection, section.nextSibling);
								}else
									GENTICS.Aloha.Log.warn(GENTICS.Aloha.Sections, "this feature is not available with multiple choice or out of section");
							},
							tooltip : that.i18n("button.addNextSection"),
							toggle : false
						})
	
	GENTICS.Aloha.FloatingMenu.addButton(scope, button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 2);
	this.buttonsSection["addnextsection"] = button
	
	button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button codeBtn",
							size : "small",
							//label : that.i18n("button.SectionCode"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								var code = GENTICS.Aloha.Sections.findCodeMarkup(range);
								if( range.isCollapsed() ){
										
									var newSection = jQuery("<code><pre>"+ that.i18n("message.newCode") +"</pre></code>");
									code = jQuery("<pre></pre>");
									GENTICS.Utils.Dom.insertIntoDOM(code, range, jQuery(GENTICS.Aloha.activeEditable.obj));
									code = code.get(0);
									code.parentNode.replaceChild(newSection.get(0),code);
									GENTICS.Aloha.Sections.InitSecionCode(newSection.get(0));
									newSection.click();
									
								}else
									GENTICS.Aloha.Log.warn(GENTICS.Aloha.Sections, "this feature is not available with multiple choice");
								
							},
							tooltip : that.i18n("button.SectionCode"),
							toggle : false
						});
	
	GENTICS.Aloha.FloatingMenu.addButton(scope, button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 1);
	this.buttonCode = button;

};

 